package com.mine.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mine.common.utils.IpUtils;
import com.mine.common.utils.SpringContextUtils;
import com.mine.common.utils.oConvertUtils;
import com.mine.modules.sys.entity.SysRolePermission;
import com.mine.modules.sys.mapper.SysRolePermissionMapper;
import com.mine.modules.sys.service.ISysRolePermissionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mine.modules.sys.utils.ListUtils;
import com.mine.modules.vm.common.constant.BizConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 角色权限表 服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-09
 */
@Service
public class SysRolePermissionServiceImpl extends ServiceImpl<SysRolePermissionMapper, SysRolePermission> implements ISysRolePermissionService {

    @Resource
    SysRolePermissionMapper sysRolePermissionMapper;

    @Override
    public void saveRolePermission(String roleId, String permissionIds) {
        String ip = "";
        try {
            //获取request
            HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
            //获取IP地址
            ip = IpUtils.getIpAddr(request);
        } catch (Exception e) {
            ip = "127.0.0.1";
        }
        LambdaQueryWrapper<SysRolePermission> query = new QueryWrapper<SysRolePermission>().lambda().eq(SysRolePermission::getRoleId, roleId);
        this.remove(query);
        List<SysRolePermission> list = new ArrayList<SysRolePermission>();
        String[] arr = permissionIds.split(",");
        for (String p : arr) {
            if (oConvertUtils.isNotEmpty(p)) {
                SysRolePermission rolepms = new SysRolePermission(roleId, p);
                rolepms.setOperateDate(new Date());
                rolepms.setOperateIp(ip);
                list.add(rolepms);
            }
        }
        this.saveBatch(list);
    }

    @Override
    public void saveRolePermission(String roleId, String permissionIds, String lastPermissionIds) {
        String ip = "";
        try {
            //获取request
            HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
            //获取IP地址
            ip = IpUtils.getIpAddr(request);
        } catch (Exception e) {
            ip = "127.0.0.1";
        }
        List<String> add = ListUtils.getDiff(lastPermissionIds, permissionIds);
        if (add != null && !add.isEmpty()) {
            List<SysRolePermission> list = new ArrayList<SysRolePermission>();
            for (String p : add) {
                if (oConvertUtils.isNotEmpty(p)) {
                    SysRolePermission sysRolePermission = lambdaQuery()
                            .eq(SysRolePermission::getRoleId, roleId)
                            .eq(SysRolePermission::getPermissionId, p).one();
                    if(sysRolePermission==null){
                        SysRolePermission rolepms = new SysRolePermission(roleId, p);
                        rolepms.setOperateDate(new Date());
                        rolepms.setOperateIp(ip);
                        list.add(rolepms);
                    }
                }
            }
            this.saveBatch(list);
        }

        List<String> delete = ListUtils.getDiff(permissionIds, lastPermissionIds);
        if (delete != null && delete.size() > 0) {
            for (String permissionId : delete) {
                this.remove(new QueryWrapper<SysRolePermission>().lambda().eq(SysRolePermission::getRoleId, roleId).eq(SysRolePermission::getPermissionId, permissionId));
            }
        }
    }

    @Override
    public List<SysRolePermission> queryRolePermission(String userId) {
        return sysRolePermissionMapper.queryRolePermission(userId);
    }

}
